package com.dstz.demo.rest.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dstz.base.api.aop.annotion.CatchErr;
import com.dstz.base.api.response.impl.ResultMsg;
import com.dstz.base.core.id.IdUtil;
import com.dstz.base.core.util.RestTemplateUtil;
import com.dstz.base.core.util.StringUtil;
import com.dstz.base.rest.BaseController;
import com.dstz.bpm.api.model.remote.BpmRemoteBusinessData;
import com.dstz.bpm.api.model.remote.FormHandlerResult;
import com.dstz.bus.api.remote.RemoteBusinessData;
import com.dstz.demo.core.manager.DemoManager;
import com.dstz.demo.core.model.Demo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 案例 控制器类<br/>
 * 
 * @author aschs
 *         </pre>
 */
@RestController
@RequestMapping("/demo/demo/")
@Api(description = "DEMO 案例模块")
public class DemoController extends BaseController<Demo> {
	@Resource
	DemoManager demoManager;

	@Override
	protected String getModelDesc() {
		return "案例";
	}
	
	@RequestMapping("saveData")
	@CatchErr(value = "saveData异常")
	public ResultMsg<String> saveData(@RequestBody RemoteBusinessData<Demo> remoteBusinessData) throws Exception {
		LOG.info("saveData remoteBusinessData:{}", JSON.toJSONString(remoteBusinessData));
		
		Demo demo = remoteBusinessData.getData();
		if (StringUtil.isEmpty(demo.getId())) {
			demoManager.create(demo);
		} else {
			demoManager.update(demo);
		}

		return getSuccessResult(demo.getId());
	}

	@RequestMapping("loadData")
	@CatchErr(value = "loadData异常")
	public ResultMsg<RemoteBusinessData<Demo>> loadData(@RequestBody RemoteBusinessData remoteBusinessData) throws Exception {
		LOG.info("loadData remoteBusinessData:{}", JSON.toJSONString(remoteBusinessData));
		
		Demo demo = demoManager.get(String.valueOf(remoteBusinessData.getId()));
		
		RemoteBusinessData<Demo> data = new RemoteBusinessData<>();
		data.setData(demo);
		
		return getSuccessResult(data);
	}

	@RequestMapping("removeData")
	@CatchErr(value = "removeData异常")
	public ResultMsg<String> removeData(@RequestBody RemoteBusinessData remoteBusinessData) throws Exception {
		LOG.info("removeData remoteBusinessData:{}", JSON.toJSONString(remoteBusinessData));
		
		demoManager.remove(String.valueOf(remoteBusinessData.getId()));
		
		return getSuccessResult();
	}

	@PostMapping("formHandler")
	@CatchErr
	@ApiOperation(value = "Demo 远程表单处理器", notes = "远程表单处理器，配合Demo Url表单使用 ，如:bpmScriptUtil.httpFormHandler(\"http://localhost:8080/agile-bpm-platform/demo/demo/formHandler\")")
	public ResultMsg<FormHandlerResult> formHandler(@RequestBody BpmRemoteBusinessData<Demo> remoteBusinessData) throws Exception {
		LOG.debug("DEMO 表单处理器执行保存动作===》{}", JSON.toJSONString(remoteBusinessData));

		Demo demo = remoteBusinessData.getData();
		FormHandlerResult formHandlerResult = new FormHandlerResult();
		// 第一次保存，构建业务主键关联
		if (StringUtil.isEmpty(demo.getId())) {
			String id = IdUtil.getSuid();
			// 这里必须返回 业务主键
			formHandlerResult.setBizId(id);
			demo.setId(id);
			demoManager.create(demo);

			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("startVariable", demo.getMz());
			// 启动时候设置一些流程变量，请看 act_ru_variable 表、在整个流程声明周期您都可以使用该流程变量，可以用于分支判断等等
			formHandlerResult.setVariables(hashMap);
		} else {
			// 更新情况
			demoManager.update(demo);
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("doTaskVariable", demo.getMz() + "-" + remoteBusinessData.getActionName());
			formHandlerResult.setVariables(hashMap);
		}

		ResultMsg<FormHandlerResult> resultMsg = new ResultMsg<>();
		resultMsg.setOk(true);
		resultMsg.setData(formHandlerResult);
		return resultMsg;
	}

	@RequestMapping("testRemote")
	@CatchErr(write2response = true, value = "testRemote失败")
	public void testRemote(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String url = "http://localhost:8080/agile-bpm-platform/demo/demo/saveData";
		RemoteBusinessData<JSONObject> remoteBusinessData = new RemoteBusinessData<>();
		ResultMsg<JSONObject> resp = RestTemplateUtil.post(url, remoteBusinessData, ResultMsg.class);
		System.out.println(resp);
	}

	
	
	public static void main(String[] args) {
		RestTemplate restTemplate = new RestTemplate();
		String url = "http://localhost:8080/agile-bpm-platform/bus/businessTable/listJson";
		HttpHeaders headers = new HttpHeaders();
		headers.add("Cookie", "JSESSIONID=5AB79F7C31EE04FEB34EEFADA8B1C726;JSESSIONID=A9E81C4FC673DABE48F53D6E722DD758");
		HttpEntity<String> entity = new HttpEntity<>("abc", headers);
		ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class);
		System.out.println(responseEntity.getBody());
	}
}
